package cn.agiledata.bank.myspace.action;

import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.hibernate.Session;

import cn.agiledata.bank.account.service.AccountSummary;
import cn.agiledata.bank.account.service.IReturnCodeHolder;
import cn.agiledata.bank.common.util.Constant;
import cn.agiledata.bank.common.util.UniqueIdentityUtil;
import cn.agiledata.bank.common.util.WeatherUtil;
import cn.agiledata.bank.common.dao.AccountTemplate;
import cn.agiledata.bank.common.dao.AccountTemplateInter;
import cn.agiledata.bank.common.exception.BusinessException;
import cn.agiledata.bank.hibernateBean.Account;
import cn.agiledata.bank.transaction.form.FillMoneyForm;
import com.vnv.infrastructure.exception.IPBException;
import com.vnv.infrastructure.logic.IPBMgrFactory;

/**
 * 
 * @author jessik
 *
 */
public class SubAccountManagerAction extends AbstractMySpaceAction {
	/**
	 * 子账户列表
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws BusinessException
	 */
	public ActionForward subAccount(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	throws IPBException, Exception {
		AccountSummary user = (AccountSummary) request.getSession(true)
				.getAttribute(Constant.SESSION_ACCOUNT_SUMMARY);
		if (user == null) {
			this.setReturnCode(request, IReturnCodeHolder.HASNOT_LOGON,SUB_ACCOUNT_LIST);
			return mapping.findForward(FAILURE);
		}
		//
		long accountId = WeatherUtil.getAccountIdFromSession(request);
		Account account = getMySpaceService().getAccount(String.valueOf(accountId));
		
		request.setAttribute("accountinfo",account);
		
		
		//
		String defaultSubaccountId = request.getParameter("key");
		if (defaultSubaccountId != null) {
			defaultSubaccountId = defaultSubaccountId.trim();
			try {
				user.changeDefaultSubAccount(defaultSubaccountId);
			} catch (BusinessException e) {
				log.error(e);
				this.setReturnCode(request, e.getReturnCode(),SUB_ACCOUNT_LIST);
				return mapping.findForward(FAILURE);
			}
		}

		return mapping.findForward(SUCCESS);

	}
	
	/**
	 * 新增子账户
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws IPBException
	 * @throws Exception
	 */
	public ActionForward addSubAccount(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	throws IPBException, Exception {
		String subAccountId = ((DynaActionForm)form).getString("subAccountId").trim();
		String subAccountName = ((DynaActionForm)form).getString("subAccountName");
		String detailFlag = ((DynaActionForm)form).getString("detailFlag");
		String subAccountRemark = ((DynaActionForm)form).getString("subAccountRemark");
//		String createUser = ((DynaActionForm)form).getString("createUser");
		String setRemark = null;
		
		if(subAccountRemark != null){
			if(subAccountRemark.length()>255){
				setRemark = subAccountRemark.substring(0,180);
			}else{
				setRemark = subAccountRemark;
			}
		}
		
		AccountSummary user = (AccountSummary) request.getSession(true)
				.getAttribute(Constant.SESSION_ACCOUNT_SUMMARY);
		if (user == null) {
			this.setReturnCode(request, IReturnCodeHolder.HASNOT_LOGON,ADD_SUB_ACCOUNT);
			return mapping.findForward(FAILURE);
		}

		if (subAccountId!= null && !subAccountId.equals("")){
			try {
				user.addSubAccount(subAccountId, subAccountName, detailFlag,setRemark);
			} catch (BusinessException e) {
				log.error(e);
				this.setReturnCode(request,e.getReturnCode(),ADD_SUB_ACCOUNT);
				return mapping.findForward(FAILURE);
			}
		}
		return mapping.findForward(SUCCESS);	
	}

	/**
	 * 修改子账户
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws IPBException
	 * @throws Exception
	 */
	public ActionForward modifySubAccount(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IPBException, Exception {
		
		
		String id = ((DynaActionForm)form).getString("subId").trim();
		String subAccountId = ((DynaActionForm)form).getString("subAccountId").trim();
		String subAccountName = ((DynaActionForm)form).getString("subAccountName");
		String detailFlag = ((DynaActionForm)form).getString("detailFlag");
		String subAccountRemark = ((DynaActionForm)form).getString("subAccountRemark");
		String setRemark = null;
		
		if(subAccountRemark != null){
			if(subAccountRemark.length()>255){
				setRemark = subAccountRemark.substring(0,180);
			}else{
				setRemark = subAccountRemark;
			}
		}
		
		
		AccountSummary user = (AccountSummary) request.getSession(true).getAttribute(Constant.SESSION_ACCOUNT_SUMMARY) ;
		if (user == null) {
			this.setReturnCode(request, IReturnCodeHolder.HASNOT_LOGON,MODIFY_SUB_ACCOUNT);
			return mapping.findForward(FAILURE);
		}
		try {
			user.updateSubAccount(id,subAccountId,subAccountName,detailFlag,setRemark) ;
			String str = "debug" ;
		} catch (BusinessException e) {
			log.error(e);
			this.setReturnCode(request, e.getReturnCode(),MODIFY_SUB_ACCOUNT);
			return mapping.findForward(FAILURE);
		}
		
		request.setAttribute("subAccountId", subAccountId) ;
		request.setAttribute("subAccountName", subAccountName) ;
		request.setAttribute("subAccountRemark", setRemark) ;
		return mapping.findForward(SUCCESS);
	}
	
	/**
	 * 删除子账户
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws IPBException
	 * @throws Exception
	 */
	public ActionForward deleteSubAccount(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	throws IPBException, Exception {
		String subAccountId = request.getParameter("key").trim() ;
		AccountTemplateInter at = (AccountTemplateInter)IPBMgrFactory.getMgr("accountTemplate");
		List l = at.getTran(subAccountId);
		if(l.isEmpty()==false){
			request.getSession().setAttribute("delError","不能删除子账户，有尚未完成的交易");
			return mapping.findForward("error");
		}
		
		AccountSummary user = (AccountSummary) request.getSession(true).getAttribute(Constant.SESSION_ACCOUNT_SUMMARY) ;
		if (user == null) {
			this.setReturnCode(request, IReturnCodeHolder.HASNOT_LOGON,DELETE_SUB_ACCOUNT);
			return mapping.findForward(FAILURE);
		}
		try {
			user.deleteSubAccount(user.getDefaultSubAccountId(),Long.parseLong(subAccountId)) ;
			
		} catch (BusinessException e) {
			log.error(e);
			this.setReturnCode(request, e.getReturnCode(),DELETE_SUB_ACCOUNT);
			return mapping.findForward(FAILURE);
		}
		return mapping.findForward(SUCCESS);		
	}
	
	/**
	 * 设置默认子账户
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws IPBException
	 * @throws Exception
	 */
	public ActionForward changeDefaultSubAccount(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	throws IPBException, Exception {
		AccountSummary user = (AccountSummary) request.getSession(true)
				.getAttribute(Constant.SESSION_ACCOUNT_SUMMARY);

		if (user == null) {
			this.setReturnCode(request, IReturnCodeHolder.HASNOT_LOGON,CHANGE_DEFAULT_SUB_ACCOUNT);
			return mapping.findForward(FAILURE);		
		}

		//
		long accountId = WeatherUtil.getAccountIdFromSession(request);
		Account account = getMySpaceService().getAccount(String.valueOf(accountId));
		
		request.setAttribute("accountinfo",account);
		
		
		//
		
		
		String defaultSubaccountId = request.getParameter("key");
		
		if (defaultSubaccountId != null) {
			defaultSubaccountId = defaultSubaccountId.trim();
			try {
				user.changeDefaultSubAccount(defaultSubaccountId);
				
			} catch (BusinessException e) {
				log.error(e);
				this.setReturnCode(request, e.getReturnCode(),CHANGE_DEFAULT_SUB_ACCOUNT);
				return mapping.findForward(FAILURE);		
			}
		}
		return mapping.findForward(SUCCESS);		

	}
	
	/**
	 * 产生默认子账户id
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws IPBException
	 * @throws Exception
	 */
	public ActionForward defSubAccountId(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws IPBException, Exception {
		AccountSummary user = (AccountSummary) request.getSession(true)
		.getAttribute(Constant.SESSION_ACCOUNT_SUMMARY);
		String defSubAccountId = UniqueIdentityUtil.getSubAccountId(String.valueOf(user.getAccountId()));
		request.setAttribute("defSubAccountId",defSubAccountId);
		return mapping.findForward(request.getParameter("next"));
	}

	

}
